package net.sureon.web.user;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sureon.app.UserSession;
import net.sureon.biz.DailyTimingMgr;
import net.sureon.biz.UserBO;
import net.sureon.biz.UserMgr;
import net.sureon.common.SureonConstants;
import net.sureon.dao.DailyTiming;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

/**
 * don't review dailytiming
 * @author Administrator
 * 
 */
public class UnderlingDailyTiming implements Controller {

	private DailyTimingMgr dailyTimingMgr;
	private UserMgr userMgr;
	
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		 long userID = ((net.sureon.app.UserSession) request.getSession()
		 .getAttribute(UserSession.HTTP_SESSION_KEY)).getUserID();

		long companyID = ((net.sureon.app.UserSession) request.getSession()
				.getAttribute(UserSession.HTTP_SESSION_KEY)).getCompanyID();

		long groupID = ((net.sureon.app.UserSession) request.getSession()
				.getAttribute(UserSession.HTTP_SESSION_KEY)).getGroupID();
		
		
		String showContent = request.getParameter("showContent");
		if("true".equals(showContent)) {
			// 表示显示具体内容
			long dailyTimingID = Long.parseLong(request.getParameter("dailyTimingID"));
			DailyTiming dailyTiming = dailyTimingMgr.getDailyTimingByID(companyID, dailyTimingID);
			if(dailyTiming != null) {
				request.setAttribute("dailyTiming", dailyTiming);
			}
		}
		

		List<Long> idsAsList = userMgr.getChildUsersWithConfiguredLevel(companyID, userID);
		
		String cp = request.getParameter("currentPage");
		int currentPage = 1;
		if(!"".equals(cp)) {
			try {
				currentPage = Integer.parseInt(request.getParameter("currentPage"));
			} catch(Exception e) {
				
			}
		}
		int maxResults = SureonConstants.PAGE_SIZE;
		int skipResults = (currentPage - 1) * maxResults;
		
		List<DailyTiming> all = null;
		all = dailyTimingMgr.getDailyTimingByGroupNotReview(companyID, idsAsList, skipResults, maxResults);
		
		UserBO user = null;
		for(DailyTiming dt : all) {
			user = userMgr.getUser(companyID, dt.getUserID());
			dt.setAuthor(user.getUserVO());
		}

		request.setAttribute("all", all);
		int pageCounter = (dailyTimingMgr.getDailyTimingByGroupNotReview(companyID, idsAsList, 0, 10000).size() + SureonConstants.PAGE_SIZE - 1) / SureonConstants.PAGE_SIZE;
		request.setAttribute("currentPage", currentPage);
		request.setAttribute("pageCounter", pageCounter);

		return new ModelAndView("user/underlingDailyTiming");
	}

	public DailyTimingMgr getDailyTimingMgr() {
		return dailyTimingMgr;
	}

	public void setDailyTimingMgr(DailyTimingMgr dailyTimingMgr) {
		this.dailyTimingMgr = dailyTimingMgr;
	}

	public UserMgr getUserMgr() {
		return userMgr;
	}

	public void setUserMgr(UserMgr userMgr) {
		this.userMgr = userMgr;
	}
}
